from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from fastapi import (APIRouter, Form, UploadFile, File, Query, Path, HTTPException,
                     BackgroundTasks, WebSocket, WebSocketDisconnect)

router = APIRouter(prefix='/jsj')


class STTRequest(BaseModel):
    format: Optional[str] = "wav"
    sample_rate: Optional[int] = 16000
    enable_punctuation_prediction: Optional[bool] = True
    enable_inverse_text_normalization: Optional[bool] = True


# str_a = ("设备类巡视项，进站闸机_14 显示屏正常显示<br /> "
#        "环境类巡视项，A站口通道，有拖大件行字乘客 <button class=’view-btn’  onclick='showAlarm(123)'>查看</button><br />"
#        "环境炎邋视项，上行站台位萱，有乘客出现共倒  <button class=’view-btn’  onclick='showAlarm(124)'>查看</button><br />"
#        "环境类進视项，B口出站闹机，出现非法闯闸泰客  <button class=’view-btn’  onclick='showAlarm(125)'>查看</button><br />"
#        "巡站结果已自动生成台账，巡站异常提醒正在发送给厅巡岗【XXX】现场确认。")

str_a = ("开始智能巡站...<br>"

         "<br>启动设备类巡视<br>"
         "-->闸机监测<br>"
         "闸机功能正常、机壳无损伤、外观清洁、显示灯正常、显示屏显示，正常<br>"
         "-->售票机监测<br>"
         "售票机机壳无损伤、外观清洁、显示灯正常、功能是否正常，正常<br>"
         "-->扶梯监测<br>"
         "扶梯运行方向是否正确、功能是否正常，正常<br>"
         "-->PIS监测<br>"
         "PIS功能是否正常、显示屏是否正常，正常<br>"
         "-->滑门监测<br>"
         "滑门功能是否正常，正常<br>"


         "<br>启动环境类巡视<br>"
         "-->治安监测<br>"
         "站内治安是否正常，正常<br>"
         "-->出入口监测<br>"
         "地面是否积水、人员是否聚集、有无垃圾<br>"
         "-->清洁监测<br>"
         "地面积水杂物、扶梯楼梯、墙/柱、垃圾箱，正常<br>"

         "<br>-->消防<br>"
         "华山A口外安全出口通道发现电动车，<button class='view-btn'  onclick='showAlarm(10001)'>查看</button></br>"

         "<br>消防安全疏散标志、应急防火门处于关闭、消防器材和消防安全标志是否在位、加封、数量无缺、状态良好，正常<br>"

         "<br>-->人员在岗状态<br>"
         "B端安检发现人员离岗，<button class=’view-btn’  onclick='showAlarm(10002)'>查看</button></br>"


         "<br>————————————————————————<br>"
         "智能巡站已结束，用时35秒，巡站结果已自动生成台账，巡站异常提醒正在发送给厅巡岗【李秋】、站务【王铁】，进行现场确认。<br>")

str_b = ("厅巡岗【李秋】已收到，正在前往。<br>"
         "巡检任务结束。")

alarm_10001 = {"alarm_time": "2024年12月25日",
               "location": "华山A口外安全出口通道", "alarm_pic": "http://192.168.227.78:30080/video/1_1.jpg",
               "alarm_video": "http://192.168.227.78:30080/video/1_2.mp4", "alarm_name": "电动车识别告警"}
alarm_10002 = {"alarm_time": "2024年12月25日",
               "location": "B端安检", "alarm_pic": "http://192.168.227.78:30080/video/2_1.jpg",
               "alarm_video": "http://192.168.227.78:30080/video/2_2.mp4", "alarm_name": "人员离岗告警"}
alarm_10003 = {"alarm_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
               "location": "10003", "alarm_pic": "http://localhost:1001/pic/10003.png",
               "alarm_video": "", "alarm_name": "10003"}
alarm_10004 = {"alarm_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
               "location": "10004", "alarm_pic": "http://localhost:1001/pic/10004.png",
               "alarm_video": "", "alarm_name": "10004"}
alarm_10005 = {"alarm_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
               "location": "10005", "alarm_pic": "http://localhost:1001/pic/10005.png",
               "alarm_video": "", "alarm_name": "10005"}
alarms = {
    "10001": alarm_10001,
    "10002": alarm_10002,
    "10003": alarm_10003,
    "10004": alarm_10004,
    "10005": alarm_10005
}

# 全局值
global_value = 0

global_dict = {}


# 用于更新全局值的模型
class UpdateValue(BaseModel):
    value: int


class UpdateDict(BaseModel):
    value: dict


@router.get("/chat")
async def chat(chat_id: Optional[str] = Query(default=None, description="会话id"),
               content: str = Query(description="内容")):
    return_str = ""
    if content == '开始自动巡检':
        return_str = str_a
    elif content == '确认':
        return_str = str_b
        global global_value
        global_value = 1
    else:
        return_str = '模型有误'

    return {"code": 200, "data": {"content": return_str}}


@router.get("/alarm")
def get_alarm(alarm_id: str = Query(description="告警编号")):
    if alarm_id in alarms:
        return {"code": 200, "data": alarms[alarm_id]}
    else:
        return {"code": 404, "error": "Alarm not found"}


@router.get("/value")
def get_value():
    return {"value": global_value}


@router.post("/update_value")
def update_value(update: UpdateValue):
    global global_value
    global_value = update.value
    return {"message": "Value updated successfully", "new_value": global_value}


@router.get("/dict")
def get_dict():
    return global_dict


@router.post("/update_dict")
def update_dict(update: UpdateDict):
    global global_dict
    global_dict = update.value
    return {"message": "Value updated successfully", "new_value": global_dict}

# @router.post("/stt")
# async def speech_to_text(
#     file: UploadFile = File(...),
#     format: Optional[str] = Form("wav"),
#     sample_rate: Optional[int] = Form(16000),
#     enable_punctuation_prediction: Optional[bool] = Form(True),
#     enable_inverse_text_normalization: Optional[bool] = Form(True)
# ):
